#include "server.h"

//////////////////////////////////////////////////////////////////
/// 辅助函数
//////////////////////////////////////////////////////////////////
MicroIMS::UserInfo makeUserInfo(int index, const QByteArray &avatar)
{
    MicroIMS::UserInfo userInfo;
    userInfo.setUserId(QString::number(1000 + index));
    userInfo.setNickname("张三" + QString::number(index));
    userInfo.setDescription("个性签名" + QString::number(index));
    userInfo.setPhone("12345678901");
    userInfo.setAvatar(avatar);
    return userInfo;
}

MicroIMS::MessageInfo makeTextMessageInfo(int index, const QString &chatSessionId, const QByteArray &avatar)
{
    MicroIMS::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    MicroIMS::StringMessageInfo stringMessageInfo;
    stringMessageInfo.setContent("这是一条测试消息" + QString::number(index));

    MicroIMS::MessageContent messageContent;
    messageContent.setMessageType(MicroIMS::MessageTypeGadget::MessageType::STRING);
    messageContent.setStringMessage(stringMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

MicroIMS::MessageInfo makeImageMessageInfo(int index, const QString &chatSessionId, const QByteArray &avatar)
{
    MicroIMS::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    MicroIMS::ImageMessageInfo imageMessageInfo;
    imageMessageInfo.setFileId("testImage");

    MicroIMS::MessageContent mesageContent;
    mesageContent.setMessageType(MicroIMS::MessageTypeGadget::MessageType::IMAGE);
    mesageContent.setImageMessage(imageMessageInfo);
    messageInfo.setMessage(mesageContent);
    return messageInfo;
}

MicroIMS::MessageInfo makeFileMessageInfo(int index, const QString &chatSessionId, const QByteArray &avatar)
{
    MicroIMS::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    MicroIMS::FileMessageInfo fileMessageInfo;
    fileMessageInfo.setFileId("testFile");

    MicroIMS::MessageContent mesageContent;
    mesageContent.setMessageType(MicroIMS::MessageTypeGadget::MessageType::FILE);
    mesageContent.setFileMessage(fileMessageInfo);
    messageInfo.setMessage(mesageContent);
    return messageInfo;
}

MicroIMS::MessageInfo makeSpeechMessageInfo(int index, const QString &chatSessionId, const QByteArray &avatar)
{
    MicroIMS::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    MicroIMS::SpeechMessageInfo speechMessageInfo;
    speechMessageInfo.setFileId("testSpeech");

    MicroIMS::MessageContent mesageContent;
    mesageContent.setMessageType(MicroIMS::MessageTypeGadget::MessageType::SPEECH);
    mesageContent.setSpeechMessage(speechMessageInfo);
    messageInfo.setMessage(mesageContent);
    return messageInfo;
}

//////////////////////////////////////////////////////////////////
/// HTTP 服务器
//////////////////////////////////////////////////////////////////
HttpServer *HttpServer::instance = nullptr;

HttpServer *HttpServer::getInstance()
{
    if (instance == nullptr)
    {
        instance = new HttpServer();
    }
    return instance;
}

bool HttpServer::init()
{
    int ret = _httpServer.listen(QHostAddress::Any, 8000);
    // 配置路由
    _httpServer.route("/ping",
                      [](const QHttpServerRequest &req)
                      {
                          (void) req;
                          qDebug() << "[http] 收到 ping 请求";
                          return "pong";
                      });
    _httpServer.route("/service/user/get_user_info",
                      [=](const QHttpServerRequest &req) { return this->getUserInfo(req); });

    _httpServer.route("/service/friend/get_friend_list",
                      [=](const QHttpServerRequest &req) { return this->getFriendList(req); });

    _httpServer.route("/service/friend/get_chat_session_list",
                      [=](const QHttpServerRequest &req) { return this->getChatSessionList(req); });

    _httpServer.route("/service/friend/get_pending_friend_events",
                      [=](const QHttpServerRequest &req) { return this->getApplyList(req); });

    _httpServer.route("/service/message_storage/get_recent",
                      [=](const QHttpServerRequest &req) { return this->getRecent(req); });

    _httpServer.route("/service/message_transmit/new_message",
                      [=](const QHttpServerRequest &req) { return this->newMessage(req); });

    _httpServer.route("/service/user/set_nickname",
                      [=](const QHttpServerRequest &req) { return this->setNickname(req); });

    _httpServer.route("/service/user/set_description",
                      [=](const QHttpServerRequest &req) { return this->setDesc(req); });

    _httpServer.route("/service/user/get_phone_verify_code",
                      [=](const QHttpServerRequest &req) { return this->getPhoneVerifyCode(req); });

    _httpServer.route("/service/user/set_phone", [=](const QHttpServerRequest &req) { return this->setPhone(req); });

    _httpServer.route("/service/user/set_avatar", [=](const QHttpServerRequest &req) { return this->setAvatar(req); });

    _httpServer.route("/service/friend/remove_friend",
                      [=](const QHttpServerRequest &req) { return this->removeFriend(req); });

    _httpServer.route("/service/friend/add_friend_apply",
                      [=](const QHttpServerRequest &req) { return this->addFriendApply(req); });

    return ret == 8000;
}

QHttpServerResponse HttpServer::getUserInfo(const QHttpServerRequest &req)
{
    // 解析请求，把 req 的body 取出来，并且通过 pb 进行反序列化
    MicroIMS::GetUserInfoReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 获取用户信息] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应数据
    MicroIMS::GetUserInfoRsp pbResp;
    pbResp.setRequestId(pbResp.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    MicroIMS::UserInfo userInfo;
    userInfo.setUserId("1029");
    userInfo.setNickname("张三");
    userInfo.setPhone("12345678901");
    userInfo.setAvatar(loadFileToByteArray(":/resource/image/groupAvatar.png"));
    pbResp.setUserInfo(userInfo);

    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应数据
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getFriendList(const QHttpServerRequest &req)
{
    // 解析请求，把 req 的 body 取出来
    MicroIMS::GetFriendListReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 获取好友列表] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    MicroIMS::GetFriendListRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    // 从文件读取数据
    QByteArray avatar = loadFileToByteArray(":/resource/image/defaultAvatar.png");
    for (int i = 0; i < 20; i++)
    {
        MicroIMS::UserInfo userInfo = makeUserInfo(i, avatar);
        pbRsp.friendList().push_back(userInfo);
    }

    // 进行序列化
    QByteArray body = pbRsp.serialize(&_serializer);

    // 构造成 HTTP 响应对象
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionList(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::GetChatSessionListReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 获取会话列表] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    MicroIMS::GetChatSessionListRsp pbResp;
    pbResp.setRequestId(pbResp.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray avatar = loadFileToByteArray(":/resource/image/defaultAvatar.png");

    // 构造若干个单聊会话
    for (int i = 0; i < 30; i++)
    {
        MicroIMS::ChatSessionInfo chatSessionInfo;
        chatSessionInfo.setChatSessionId(QString::number(2000 + i));
        chatSessionInfo.setChatSessionName("会话" + QString::number(i));
        chatSessionInfo.setSingleChatFriendId(QString::number(1000 + i));
        chatSessionInfo.setAvatar(avatar);
        MicroIMS::MessageInfo messageInfo = makeTextMessageInfo(i, chatSessionInfo.chatSessionId(), avatar);

        chatSessionInfo.setPrevMessage(messageInfo);

        pbResp.chatSessionInfoList().push_back(chatSessionInfo);
    }

    // 序列化成字节数组
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应对象
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::getApplyList(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::GetPendingFriendEventListReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 获取好友申请列表] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    MicroIMS::GetPendingFriendEventListRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    // 循环构造出 event 对象，构造出整个结果数组

    QByteArray avatar = loadFileToByteArray(":/resource/image/defaultAvatar.png");

    for (int i = 0; i < 5; i++)
    {
        MicroIMS::FriendEvent friendEvent;
        friendEvent.setEventId(""); // 此标记已弃用
        friendEvent.setSender(makeUserInfo(i, avatar));

        pbResp.event().push_back(friendEvent);
    }

    // 序列化成字节数组
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应对象
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::getRecent(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::GetRecentMsgReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 获取最近消息列表] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ",chatSessionId=" << pbReq.chatSessionId();

    // 构造响应
    MicroIMS::GetRecentMsgRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray avatar = loadFileToByteArray(":/resource/image/defaultAvatar.png");

    for (int i = 0; i < 30; i++)
    {
        MicroIMS::MessageInfo messageInfo = makeTextMessageInfo(i, "2000", avatar);
        pbResp.msgList().push_back(messageInfo);
    }
    MicroIMS::MessageInfo imageMessageInfo = makeImageMessageInfo(30, "2000", avatar);
    pbResp.msgList().push_back(imageMessageInfo);
    MicroIMS::MessageInfo fileMessageInfo = makeFileMessageInfo(31, "2000", avatar);
    pbResp.msgList().push_back(fileMessageInfo);
    MicroIMS::MessageInfo speechMessageInfo = makeSpeechMessageInfo(32, "2000", avatar);
    pbResp.msgList().push_back(speechMessageInfo);

    // 序列化
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::newMessage(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::NewMessageReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 发送消息] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ",chatSessionId=" << pbReq.chatSessionId() << ",messageType=" << pbReq.message().messageType();

    if (pbReq.message().messageType() == MicroIMS::MessageTypeGadget::MessageType::STRING)
    {
        LOG() << "发送消息的内容=" << pbReq.message().stringMessage().content();
    }

    // 构造响应
    MicroIMS::NewMessageRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::setNickname(const QHttpServerRequest &req)
{
    MicroIMS::SetUserNicknameReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 修改用户昵称] requestId=" << pbReq.requestId()
          << ",loginSessionId=" << ",nickname=" << pbReq.nickname();

    // 构造响应
    MicroIMS::SetUserNicknameRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::setDesc(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::SetUserDescriptionReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 修改用户签名] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ",desc=" << pbReq.description();

    // 构造响应
    MicroIMS::SetUserDescriptionRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::getPhoneVerifyCode(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::PhoneVerifyCodeReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 获取短信验证码] requestId=" << pbReq.requestId() << ",phone=" << pbReq.phoneNumber();

    // 构造响应 body
    MicroIMS::PhoneVerifyCodeRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setVerifyCodeId("testVerifyCodeId");
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::setPhone(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::SetUserPhoneNumberReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 修改手机号] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", phone=" << pbReq.phoneNumber() << ", verifyCodeId=" << pbReq.phoneVerifyCodeId()
          << ", verifyCode=" << pbReq.phoneVerifyCode();

    // 构造响应 body
    MicroIMS::SetUserPhoneNumberRsp pbResp;
    pbResp.setRequestId(pbResp.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::setAvatar(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::SetUserAvatarReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 修改头像] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应 body
    MicroIMS::SetUserAvatarRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::removeFriend(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::FriendRemoveReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 删除好友] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ",peerId=" << pbReq.peerId();

    // 构造响应 body
    MicroIMS::FriendRemoveRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::addFriendApply(const QHttpServerRequest &req)
{
    // 解析请求
    MicroIMS::FriendAddReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    LOG() << "[REQ 添加好友申请] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ",userId=" << pbReq.respondentId();

    // 构造响应 body
    MicroIMS::FriendAddRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setNotifyEventId("");

    QByteArray body = pbResp.serialize(&_serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

//////////////////////////////////////////////////////////////////
/// Websocket 服务器
//////////////////////////////////////////////////////////////////
WebsocketServer *WebsocketServer::instance = nullptr;

WebsocketServer *WebsocketServer::getInstance()
{
    if (instance == nullptr)
    {
        instance = new WebsocketServer();
    }
    return instance;
}

// 针对 websocket 服务器进行初始化
bool WebsocketServer::init()
{
    // 1. 连接信号槽
    connect(&_websocketServer, &QWebSocketServer::newConnection, this,
            [=]()
            {
                qDebug() << "[websocket] 连接建立成功！";

                // 获取用来通信的 socket 对象，nextPendingConnection 类似于 socket 的 accept
                QWebSocket *socket = _websocketServer.nextPendingConnection();

                // 针对这个 socket 对象，进行剩余信号的处理
                connect(socket, &QWebSocket::disconnected, this,
                        [=]()
                        {
                            qDebug() << "[websocket] 连接断开！";
                            disconnect(this, &WebsocketServer::sendTextResp, this, nullptr);
                            disconnect(this, &WebsocketServer::sendFriendRemove, this, nullptr);
                            disconnect(this, &WebsocketServer::sendAddFriendApply, this, nullptr);
                        });

                connect(this, &WebsocketServer::sendTextResp, this,
                        [=]()
                        {
                            // 通过捕获 socket 对象给客户端返回数据
                            if (socket == nullptr || !socket->isValid())
                            {
                                LOG() << "socket 对象无效！";
                                return;
                            }
                            // 构造响应数据
                            QByteArray avatar = loadFileToByteArray(":/resource/image/defaultAvatar.png");
                            MicroIMS::MessageInfo messageInfo = makeTextMessageInfo(_messageIndex++, "2000", avatar);

                            MicroIMS::NotifyNewMessage notifyNewMessage;
                            notifyNewMessage.setMessageInfo(messageInfo);

                            MicroIMS::NotifyMessage notifyMessage;
                            notifyMessage.setNotifyEventId(""); // 已弃用
                            notifyMessage.setNotifyType(MicroIMS::NotifyTypeGadget::CHAT_SESSION_CREATE_NOTIFY);
                            notifyMessage.setNewMessageInfo(notifyNewMessage);

                            // 序列化
                            QByteArray body = notifyMessage.serialize(&_serializer);

                            // 发送消息给客户端
                            socket->sendBinaryMessage(body);

                            LOG() << "发送文本消息响应:" << messageInfo.message().stringMessage().content();
                        });

                connect(this, &WebsocketServer::sendFriendRemove, this,
                        [=]()
                        {
                            // 通过捕获 socket 对象给客户端返回数据
                            if (socket == nullptr || !socket->isValid())
                            {
                                LOG() << "socket 对象无效！";
                                return;
                            }
                            // 构造响应数据
                            MicroIMS::NotifyMessage notifyMessage;
                            notifyMessage.setNotifyEventId(""); // 已弃用
                            notifyMessage.setNotifyType(MicroIMS::NotifyTypeGadget::FRIEND_REMOVE_NOTIFY);

                            MicroIMS::NotifyFriendRemove notifyFriendRemove;
                            notifyFriendRemove.setUserId("1000");
                            notifyMessage.setFriendRemove(notifyFriendRemove);

                            QByteArray body = notifyMessage.serialize(&_serializer);
                            // 发送消息给客户端
                            socket->sendBinaryMessage(body);

                            LOG() << "通知对方好友被删除 userId=1000";
                        });

                connect(this, &WebsocketServer::sendAddFriendApply, this,
                        [=]()
                        {
                            // 通过捕获 socket 对象给客户端返回数据
                            if (socket == nullptr || !socket->isValid())
                            {
                                LOG() << "socket 对象无效！";
                                return;
                            }
                            // 构造响应数据
                            MicroIMS::NotifyMessage notifyMessage;
                            notifyMessage.setNotifyEventId(""); // 已弃用
                            notifyMessage.setNotifyType(MicroIMS::NotifyTypeGadget::FRIEND_ADD_APPLY_NOTIFY);

                            QByteArray avatar           = loadFileToByteArray(":/resource/image/defaultAvatar.png");
                            MicroIMS::UserInfo userInfo = makeUserInfo(1000, avatar);

                            MicroIMS::NotifyFriendAddApply notifyFriendAddApply;
                            notifyFriendAddApply.setUserInfo(userInfo);
                            notifyMessage.setFriendAddApply(notifyFriendAddApply);

                            QByteArray body = notifyMessage.serialize(&_serializer);
                            // 发送消息给客户端
                            socket->sendBinaryMessage(body);

                            LOG() << "通知对方好友申请数据";
                        });
            });

    // 2. 绑定端口, 启动服务
    bool ok = _websocketServer.listen(QHostAddress::Any, 8001);
    return ok;
}
